public class MySingleList {
    class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val= val;
        }

    }
    public ListNode head;
    public void Creatlinklist(){
        /*ListNode node1= new ListNode(12);
        ListNode node2= new ListNode(23);
        ListNode node3= new ListNode(34);
        ListNode node4= new ListNode(45);
        ListNode node5= new ListNode(56);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        this.head=node1;*/

    }
    public void show(){
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public int size(){
        int count=0;
        ListNode cur = head;
        while (cur!=null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    public boolean contains(int key){
        ListNode cur =head;
        while (cur!=null){
            //如果val值是引用类型，这里得用equals进行比较
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head==null){
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }
    public void addIndex(int index,int data){
        if(index>size()||index<0){
            System.out.println("插入位置不合法");
            return;
        }
        if (index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        ListNode node= new ListNode(data);
        ListNode cur = head;
        for (int i = 0; i <index-1 ; i++) {
            cur=cur.next;
        }
        node.next= cur.next;
        cur.next= node;
    }
    //删除某个元素值的结点
    public void remove(int key){
        ListNode prev= head;
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        while (prev.next!=null&&prev.next.val!=key){
            prev=prev.next;
        }
        if(prev.next==null){
            System.out.println("没有你要删除的数字");
            return;
        }
        else {
            prev.next=prev.next.next;
        }
    }
    //删除所有val值为key值的结点
    public void removeAllKey(int key){
        if (head==null){
            return;
        }

        ListNode cur = head.next;
        ListNode prev=head;
        while (cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                  cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){

            head=head.next;
        }//要么最后再判断头结点的特殊情况，要么改为while放在核心代码前面
    }


}
